package org.ma5d;

import java.util.concurrent.TimeUnit;

import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class MisFireTest {
    
    public static void main(String[] args) throws SchedulerException, InterruptedException {
        JobDetail jobDetail = JobBuilder.newJob(MisFireJob.class)
        .withIdentity("MisFireJobName", "MisFireJobGroup")
        .build();

        String cronExpression = "*/5 * * * * *";

        // doNothing 错过不执行
        Trigger doNothingTrigger = TriggerBuilder.newTrigger()
        .withIdentity("MisFireDoNothingTriggerName", "MisFireDoNothingTriggerGroup")
        .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression).withMisfireHandlingInstructionDoNothing())
        .build();

        // proceed 错过执行一次
        Trigger proceedTrigger = TriggerBuilder.newTrigger()
        .withIdentity("MisFireProceedTriggerName", "MisFireProceedTriggerGroup")
        .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression).withMisfireHandlingInstructionFireAndProceed())
        .build();

        // ignore 错过全部执行
        Trigger ignoreTrigger = TriggerBuilder.newTrigger()
        .withIdentity("MisFireIgnoreTriggerName", "MisFireIgnoreTriggerGroup")
        .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression).withMisfireHandlingInstructionIgnoreMisfires())
        .build();
        
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        
        scheduler.scheduleJob(jobDetail, doNothingTrigger);
        scheduler.scheduleJob(jobDetail, proceedTrigger);
        scheduler.scheduleJob(jobDetail, ignoreTrigger);

        scheduler.start();
        
        TimeUnit.SECONDS.sleep(10);
        scheduler.shutdown();
    }

    public static class MisFireJob implements Job{
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            
        }
    }
}
